﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using OgilvyOne.Model;
using OgilvyOne.BLL;

public partial class admin_manager_dictionary_list : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.PopedomValidate(Layerid, QUERY);
            this.headers.Text = new ColumnBll().LayeridSysColumnNaiv(Layerid);
            int upid = int.Parse(Request.Params["upid"]);
            string cheader = "";
            DictionaryManageBLL dmbll = new DictionaryManageBLL();
            while (upid > 0)
            {
                DictionaryManageModel dmm = dmbll.selectDictionary(upid);
                cheader = "&nbsp;&nbsp;<img src='../images/subnav_dot.gif' />&nbsp;&nbsp;<a href=\"dictionary_list.aspx?upid=" + upid + "&layerid=" + Layerid + "\">" + dmm.Cnname + "</a>" + cheader;
                upid = dmm.Upid;
            }
            headers.Text += cheader;
            showindex();
        }
    }

    //删除按钮
    //protected void LinkButton2_Click(object sender, EventArgs e)
    //{
    //    this.PopedomValidate(layerid, DEL);
    //     string idlist = GetSelIDlist();
    //    if (idlist.Trim().Length == 0)
    //        return;
    //    new DictionaryManageBLL().deleteDictionary(idlist);

    //    DataSet ds = new DictionaryManageBLL().GetDictionary(" upid = '" + Request.Params["upid"] + "'");
    //    GridView1.DataSource = ds;
    //    GridView1.DataBind();
    //}

    //添加按钮
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        this.PopedomValidate(layerid, ADD);
        string str = Request["upid"];
        if (str == null || str == "")
        {
            Response.Redirect("dictionary_add.aspx?upid=0" + "&layerid=" + Request.Params["layerid"]);
            return;
        }
        else
        {
            Response.Redirect("dictionary_add.aspx?upid=" + str + "&layerid=" + Request.Params["layerid"]);
            return;
        }
    }

    //修改按钮
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        this.PopedomValidate(layerid, MOD);
        GridView gv = (GridView)sender;
        string cid = gv.DataKeys[e.RowIndex].Value.ToString();
        string upid = Request["upid"];
        Response.Redirect("dictionary_add.aspx?cid=" + cid + "&upid=" + upid + "&layerid=" + Request.Params["layerid"]);
        return;
    }

    //单个删除
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        this.PopedomValidate(layerid, DEL);
        DictionaryManageBLL dicbll = new DictionaryManageBLL();
        DictionaryManageModel dicmodel = new DictionaryManageModel();
        GridView gv = (GridView)sender;
        int cid = int.Parse(gv.DataKeys[e.RowIndex].Value.ToString());
        int itt = son(cid);
        if (itt==0)
        {
            dicmodel.Cid = Convert.ToInt32(cid);
            bool bo = dicbll.deleteDictionary(cid);
            if (bo == true)
            {
                Response.Write("<script>alert('删除成功!')</script>");
                showindex();
            }
            else
            {
                Response.Write("<script>alert('删除失败!')</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('该栏目下有子栏目,请删除子栏目后在删除此栏目!')</script>");
        }
    }

    //子栏目
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        string layerid = Request["layerid"];
        this.PopedomValidate(layerid,QUERY);
        GridView gv = (GridView)sender;
        string upid = gv.DataKeys[e.NewSelectedIndex].Value.ToString();
        string text=gv.Rows[e.NewSelectedIndex].Cells[2].Text.ToString();
        Session["text"] = text;
        Response.Redirect("dictionary_list.aspx?upid=" + upid + "&layerid=" + Request.Params["layerid"], true);
    }

    //查询是否有子栏目
    protected int son(int cid)
    {
        DictionaryManageBLL dicbll = new DictionaryManageBLL();
        int itt = dicbll.selectSon(cid);
        return itt;
    }

    private string GetSelIDlist()
    {
        string idlist = "";
        bool BxsChkd = false;
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox checkbox = (CheckBox)GridView1.Rows[i].FindControl("DeleteThis");
            if (checkbox != null && checkbox.Checked == true)
            {
                BxsChkd = true;
                idlist += GridView1.Rows[i].Cells[1].Text + ",";
            }
        }
        if (BxsChkd)
        {
            idlist = idlist.Substring(0, idlist.LastIndexOf(","));
        }
        return idlist;
    }



    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.PopedomValidate(layerid, QUERY);
        GridView1.PageIndex = e.NewPageIndex;
        DataSet ds = new DictionaryManageBLL().GetDictionary(" upid = '" + Request.Params["upid"] + "'");
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void btnPrev_Click(object sender, EventArgs e)
    {
        if (this.GridView1.PageIndex > 0)
        {
            this.GridView1.PageIndex = this.GridView1.PageIndex - 1;
            showindex();
        }
        else{ 
            
        }
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        if (this.GridView1.PageIndex <this.GridView1.PageCount)
        {
            this.GridView1.PageIndex = this.GridView1.PageIndex + 1;
            showindex();
        }
        else
        {

        }
    }
    protected void btnLast_Click(object sender, EventArgs e)
    {
        this.GridView1.PageIndex = this.GridView1.PageCount;
        showindex();
    }
    protected void btnGo_Click(object sender, EventArgs e)
    {
        try
        {
            int pa = int.Parse(this.txtNewPageIndex.Text.ToString());
            if (1 <= pa || pa <= this.GridView1.PageCount)
            {
                this.GridView1.PageIndex = pa - 1;
                showindex();
            }
            else {
                Response.Write("<script>alert('请输入正确的页码!')</script>");
                this.txtNewPageIndex.Text = (this.GridView1.PageIndex + 1).ToString();
            }
        }
        catch(Exception ex)
        { 
            //Response.Write("<script>alert('"+ex.ToString()+"!')</script>");
            //this.txtNewPageIndex.Text = (this.GridView1.PageIndex + 1).ToString();
        }
    }
    protected void showindex()
    {
        DataSet ds = new DictionaryManageBLL().GetDictionary(" upid = '" + Request.Params["upid"] + "'");
       GridView1.DataSource = ds;
       GridView1.DataBind();
       int num=ds.Tables[0].Rows.Count;
       int index=this.GridView1.PageIndex+1;
       int cont = this.GridView1.PageCount;
       this.label1.Text="当前第"+index.ToString()+"页    共"+cont.ToString()+"页";
       this.label2.Text = "共" + num.ToString() + "条数据";
       this.txtNewPageIndex.Text = index.ToString();
       if (GridView1.Rows.Count == 0)
       {
           DataTable dt = new DataTable();
           dt.Columns.Add("cid");
           dt.Columns.Add("cnname");
           dt.Columns.Add("enname");
           dt.Columns.Add("availabilityflag");
           dt.Columns.Add("standby1");
           dt.Rows.Add(dt.NewRow());
           GridView1.DataSource = dt;
           GridView1.DataBind();
           int columncount = dt.Columns.Count;
           GridView1.Rows[0].Cells.Clear();
           GridView1.Rows[0].Cells.Add(new TableCell());
           GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
           GridView1.Rows[0].Cells[0].Text = "没有相关信息";
           GridView1.Rows[0].Cells[0].Style.Add("text-align", "center");
       }
       if (this.GridView1.PageCount != 1)
       {
           if (this.GridView1.PageIndex == 0)
           {
               btnFirst.Visible = false;
               btnPrev.Visible = false;
               btnNext.Visible = true;
               btnLast.Visible = true;
           }
           else if (this.GridView1.PageIndex == this.GridView1.PageCount - 1)
           {
               btnFirst.Visible = true;
               btnPrev.Visible = true;
               btnNext.Visible = false;
               btnLast.Visible = false;
           }

           else
           {
               btnFirst.Visible = true;
               btnPrev.Visible = true;
               btnNext.Visible = true;
               btnLast.Visible = true;
           }
       }
       else
       {
           btnFirst.Visible = false;
           btnPrev.Visible = false;
           btnNext.Visible = false;
           btnLast.Visible = false;
           btnGo.Visible = false;
           label1.Visible = false;
           label2.Visible = false;
           txtNewPageIndex.Visible = false;
       }
    }

    protected void btnFirst_Click1(object sender, EventArgs e)
    {
        this.GridView1.PageIndex = 0;
        showindex();
    }


}
